package com.GlobalPaint.app.Adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.MyFriendBean;
import com.GlobalPaint.app.ui.App;
import com.GlobalPaint.app.ui.Home.friend.FensiFragment;
import com.GlobalPaint.app.ui.Mine.Personal.PersonalZhanshi;
import com.GlobalPaint.app.utils.Constants;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public class MyFensiAdapter extends XRecyclerView.Adapter<MyFensiAdapter.ViewHoler>{
    private List<MyFriendBean.DataBean> bean;
    private FensiFragment fensiFragment;
    public MyFensiAdapter(List<MyFriendBean.DataBean> data, FensiFragment fensiFragment) {
        bean=data;
        this.fensiFragment=fensiFragment;
    }

    @Override
    public MyFensiAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.frament_fensi_item, parent, false);
        MyFensiAdapter.ViewHoler holder = new MyFensiAdapter.ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyFensiAdapter.ViewHoler holder, final int position) {
        String Phone=bean.get(position).getUserName()==
                null?"":bean.get(position).getUserName();

        if (Phone.length() > 4) {
            String phone1 = (Phone.substring(0, 3));
            String phone2 = Phone.substring(Phone.length() - 3, Phone.length());
            holder.tv_name.setText(phone1 + "***" + phone2);
        }
        String nick=bean.get(position).getNick()==
                null?bean.get(position).getUserName():bean.get(position).getNick();

        holder.tv_nick.setText(nick);
        String Url= bean.get(position).getAvatar()== null?"wu":
                bean.get(position).getAvatar();
        if(Url.equals("wu")){
            Picasso.with(App.context).load(R.drawable.touxiang).into(holder.imge);
        }else {
            Picasso.with(App.context).load(Constants.JASON_SERVICE_URL_TOUXIANG+Url)
                    .placeholder(R.drawable.touxiang).into(holder.imge);
        }
        holder.tv_guiji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String nick= bean.get(position).getUserName();
                String jid =bean.get(position).getUserName();
                String userName= bean.get(position).getJid();
                if(nick==null){
                    nick=bean.get(position).getJid();
                }
                fensiFragment.getfensi(nick,jid,userName);
            }
        });
        holder.imge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), PersonalZhanshi.class);
                intent.putExtra("userid",bean.get(position).getUserId());
                view.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return bean.size();
    }

    public class ViewHoler extends RecyclerView.ViewHolder {

        private final TextView tv_name;
        private final TextView tv_nick;
        private final TextView tv_guiji;
        private final ImageView imge;
        //  private final Button btndel;
       // private final SwipeMenuLayout swi;
        View mview;

        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            tv_name = (TextView)itemView.findViewById(R.id.tv_name);
            tv_nick = (TextView)itemView.findViewById(R.id.tv_nick);
           // btndel = (Button)itemView. findViewById(R.id.btndel);
            //swi = (SwipeMenuLayout) itemView.findViewById(R.id.swi);
            tv_guiji = (TextView)itemView. findViewById(R.id.tv_guiji);
            imge = (ImageView) itemView.findViewById(R.id.imge);

        }
    }
}
